import {
	gbs
} from 'config/';
import { Subject } from 'rxjs/Rx';

export default {
	name: 'editbook',
	components: {},
	data() {
		return {
			fields: [{
				key: 'name',
				desc: '请输入书籍名称',
				label: '书籍名称'
			}, {
				key: 'describe',
				desc: '请输入书籍简述',
				label: '书籍简述'
			}, {
				key: 'price',
				desc: '请输入金额',
				label: '金额',
				type: 'inputnumber'
			}, {
				key: 'chapter_count',
				desc: '请输入章节数',
				label: '章节数',
				type: 'inputnumber'
			}, {
				key: 'author',
				desc: '请输入作者',
				label: '作者'
			}, {
				key: 'word_count',
				desc: '请输入字数',
				label: '字数',
				type: 'inputnumber'
			}, {
				key: 'cover',
				type: 'upload',
				label: '书籍图片'
			}],
			default_value: {
				name: '',
				describe: '',
				price: 0,
				chapter_count: 0,
				author: '',
				word_count: 0,
				cover: '',
			},
			rules: {
				name: [{
					required: true, message: '请输入书籍名', trigger: 'blur'
				}],
				describe: [{
					required: true, message: '请输入书籍简介', trigger: 'blur'
				}],
				price: [{
					required: true, message: '请输入售价', trigger: 'blur'
				}],
				chapter_count: [{
					required: true
				}],
				author: [{
					required: true, trigger: 'blur'
				}],
				word_count: [{
					required: true, trigger: 'blur'
				}],
				cover: [{
					required: true, trigger: 'blur'
				}]
			}
		}
	},
	methods: {
		onSubmit({ data, info }) {
			console.log(data, info);
			//如果没有任何文本text，就检测html中是否包含图片img，iframe,如果都没有，代表真的没有数据。
			let param = data;
			if (this.$route.query.id) {
				this.$$api_book_updateBook({
					data: {
						data: param,
						obj: this.parseObj
					},
					fn: () => {
						this.$router.push('/main/book/list');
					}
				});
			} else {
				this.$$api_book_createBook({
					data,
					fn: () => {
						this.$router.push('/main/book/list');
					}
				});
			}
		}
	},
	created() {
	},
	mounted() {
		setTimeout(() => {
			if (this.$route.query.id) {
				var data = this.$route.query.id;
				this.$$api_book_findBook({
					data,
					fn: data => {
						console.log('data:::::', data);
						this.default_value.id = data.id;
						this.default_value.name = data.name;
						this.default_value.describe = data.describe;
						this.default_value.price = data.price;
						this.default_value.chapter_count = data.chapter_count;
						this.default_value.author = data.author;
						this.default_value.word_count = data.word_count;
						this.default_value.cover = data.cover;
					}
				});
			}

		})
	},
	watch: {
		default_value: {
			deep: true,
			handler(v) {
				this.default_value = v;
			}
		}
	}
}